package susebs.pb.travel_server.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import susebs.pb.travel_server.domain.PageDto;
import susebs.pb.travel_server.entity.Hotels;
import susebs.pb.travel_server.dao.HotelsDao;
import susebs.pb.travel_server.entity.Landmarks;
import susebs.pb.travel_server.service.HotelsService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.List;

/**
 * 酒店表(Hotels)表服务实现类
 *
 * @author makejava
 * @since 2024-02-05 09:27:10
 */
@Service("hotelsService")
public class HotelsServiceImpl implements HotelsService {
    @Resource
    private HotelsDao hotelsDao;

    /**
     * 通过ID查询单条数据
     *
     * @param hotelId 主键
     * @return 实例对象
     */
    @Override
    public Hotels queryById(Long hotelId) {
        return this.hotelsDao.queryById(hotelId);
    }

    /**
     * 分页查询
     *
     * @param hotels      筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @Override
    public Page<Hotels> queryByPage(Hotels hotels, PageRequest pageRequest) {
        long total = this.hotelsDao.count(hotels);
        return new PageImpl<>(this.hotelsDao.queryAllByLimit(hotels, pageRequest), pageRequest, total);
    }


    /**
     * 分页查询
     */
    @Override
    public PageInfo queryAllHotelsByPage(PageDto pageDto) {
        if(pageDto.getPage() == null || pageDto.getSize() == null){
            pageDto.setPage(1);
            pageDto.setSize(10);
        }
        PageHelper.startPage(pageDto.getPage(), pageDto.getSize());
        List<Hotels> list = hotelsDao.queryAllHotels(pageDto.getHotels());
        PageInfo<Hotels> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 新增数据
     *
     * @param hotels 实例对象
     * @return 实例对象
     */
    @Override
    public Hotels insert(Hotels hotels) {
        this.hotelsDao.insert(hotels);
        return hotels;
    }

    /**
     * 修改数据
     *
     * @param hotels 实例对象
     * @return 实例对象
     */
    @Override
    public Hotels update(Hotels hotels) {
        this.hotelsDao.update(hotels);
        return this.queryById(hotels.getHotelId());
    }

    /**
     * 通过主键删除数据
     *
     * @param hotelId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long hotelId) {
        return this.hotelsDao.deleteById(hotelId) > 0;
    }
}
